package com.egglazy.Controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.egglazy.Dto.GoodsAddDto;
import com.egglazy.Entity.Category;
import com.egglazy.Entity.Goods;
import com.egglazy.Service.GoodsService;
import com.egglazy.Utils.Result;
import com.egglazy.Utils.ResultUtil;
import com.egglazy.Vo.GoodsVo;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


@Tag(name = "商品接口")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    private final GoodsService goodsService;
    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @Operation(summary = "添加商品信息")
    @PostMapping("/addInfo")
    public Result<String> addGoods(@RequestBody GoodsAddDto goodsAddDto) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsAddDto, goods);
        return goodsService.save(goods) ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败！");
    }

    @Operation(summary = "根据商品id删除商品信息")
    @GetMapping("/deleteById")
    public Result<String> deleteGoodsById(@RequestParam("id") String id) {
        LambdaUpdateWrapper<Goods> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Goods::getId, id).set(Goods::getStatus, 0);
        return goodsService.update(wrapper) ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败！");
    }

    @Operation(summary = "根据id更新商品信息")
    @PostMapping("/updateById/{id}")
    public Result<String> updateGoodsById(@RequestBody GoodsAddDto goodsAddDto, @PathVariable("id") String id) {
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsAddDto, goods);
        goods.setId(id);
        return goodsService.updateById(goods) ? ResultUtil.success("更新成功") : ResultUtil.fail("gen失败！");
    }

    @Operation(summary = "根据id查询商品详情")
    @GetMapping("/getById")
    public Result<GoodsVo> getGoodsById(@RequestParam("id") String id) {

        MPJLambdaWrapper<Goods> wrapper = new MPJLambdaWrapper<Goods>()
                .selectAll(Goods.class)
                .leftJoin(Category.class,Category::getId, Goods::getCategoryId,ext ->
                        ext.selectAs(Category::getTypeValue,GoodsVo::getCategoryName))
                .eq(Goods::getId, id);
        GoodsVo goodsVo = goodsService.selectJoinOne(GoodsVo.class, wrapper);
        return ResultUtil.success(goodsVo);
    }


    @Operation(summary = "获取商品列表")
    @GetMapping("/getList")
    public Result<Page<GoodsVo>> getGoodsList(@RequestParam("pageIndex") int pageIndex,@RequestParam("pageSize") int pageSize) {
        MPJLambdaWrapper<Goods> wrapper = new MPJLambdaWrapper<Goods>()
                .selectAll(Goods.class)
                .leftJoin(Category.class,Category::getId, Goods::getCategoryId,ext ->
                        ext.selectAs(Category::getTypeValue,GoodsVo::getCategoryName));
        Page<GoodsVo> goodsVoPage = goodsService.selectJoinListPage(new Page<>(pageIndex, pageSize), GoodsVo.class, wrapper);
        return ResultUtil.success(goodsVoPage);
    }



}
